home *** CD-ROM | disk | FTP | other *** search
/ Exploring Where & Why / Exploring Where & Why.iso / pc / MODULES / LESSON04 / ACT04A / common4.cst / 00031_FollowPath.Obj.ls < prev    next >
Encoding:
Text File  |  2003-05-01  |  3.8 KB  |  111 lines

  1. property ppathFollower,pdestinationList,pcurrentDestination,pboundryPoint,pwhichBoundry,ppathFollowerRoads ,pleftDiff,prightDiff,ptopDiff, pbottomDiff,pmovementDiff
  2.  
  3. on new me,pF,startDest,endDest
  4.   set ppathFollower=pF        --sprite# of object following the path
  5.   set pdestinationList=[:]     --list of points(sprites) of all destinations to be reached along the path
  6.   set pcurrentDestination=1   --destination that currently has to be reached
  7.   set ppathFollowerRoads=[]   --list of all invisible shapes(rectangles) that are layed out along the path
  8.   set pleftDiff=point(-5,0)   --by how much an object has to move to the left,right,up, and down
  9.   set prightDiff=point(5,0)
  10.   set ptopDiff=point(0,-5)
  11.   set pbottomDiff=point(0,5)
  12.   initDestinations me,startDest,endDest
  13.   return me
  14. end
  15.  
  16.  
  17. --creates the list of all points(sprites) of destination
  18. on initDestinations me,startDest,endDest
  19.   repeat with i=startDest to endDest
  20.     addProp(pdestinationList,the name of the member of sprite i,i)
  21.   end repeat
  22. end
  23.  
  24.  
  25. --creates the list of all invisible shapes(sprites) along the path
  26. on initPath me,startRoad,endRoad
  27.   repeat with i=startRoad to endRoad
  28.     add(ppathFollowerRoads,i)
  29.   end repeat
  30. end
  31.  
  32.  
  33. --called when a directional arrow is clicked. Turns an object to proper direction, sets corresponding movement differential(i.e. pleftDiff...),
  34. --and set the boundry point
  35. on setPathFollowerDirection me, directionMember
  36.   puppetSprite ppathFollower,true
  37.   set pboundryPoint=getPathBoundry(me,the name of directionMember)
  38.   set prefix="p"
  39.   set suffix="Diff"
  40.   do "set pmovementDiff=value("&prefix&pwhichBoundry&suffix&")"  --sets movement diff. based on direction 
  41.   
  42.   set the member of sprite ppathFollower=member directionMember
  43.   updateStage
  44. end
  45.  
  46.  
  47. --moves an object along the path till boundry or destination is encountered
  48. on movePathFollower me
  49.   --checks if boundry point is reached
  50.   if max(pmovementDiff) then  --if positive difference (directions: right or down) ...
  51.     if boundryIntoCoordinate(me, pwhichBoundry,ppathFollower) >= pboundryPoint then 
  52.       return(checkDestination(me))
  53.     end if
  54.   else
  55.     if boundryIntoCoordinate(me, pwhichBoundry,ppathFollower) <= pboundryPoint then 
  56.       return(checkDestination(me))
  57.     end if
  58.   end if
  59.   
  60.   set the loc of sprite ppathFollower=the loc of sprite ppathFollower + pmovementDiff
  61.   go to the frame
  62.   return("")
  63. end
  64.  
  65.  
  66.  
  67. --gets the boundry point based on direction(up:toppest boundry,down:lowest boundry,left:leftmost boundy,right:rightmost boundry)
  68. on getPathBoundry me,whichBoundry
  69.   set pwhichBoundry=whichBoundry
  70.   set roadBoundries=[]
  71.   repeat with aRoad in ppathFollowerRoads
  72.     if (sprite ppathFollower intersects aRoad) then
  73.       add(roadBoundries,boundryIntoCoordinate(me, whichBoundry,aRoad))        
  74.     end if
  75.   end repeat
  76.   case (whichBoundry) of
  77.     "right","bottom": return max(roadBoundries)
  78.     "left","top": return min(roadBoundries)
  79.   end case
  80. end
  81.  
  82.  
  83. --checks if any destination is reached, and returns frames to go to after a destination is reached
  84. on checkDestination me
  85.   repeat with dest in pdestinationList
  86.     set correctDest=getAt(pdestinationList,pcurrentDestination)
  87.     if sprite ppathFollower intersects dest and dest=correctDest then  --if correct dest. is reached ...
  88. --      puppetSprite ppathFollower,false
  89.       set pcurrentDestination=pcurrentDestination+1
  90.       return(getOne(pdestinationList,dest)&".correct")
  91.     else if sprite ppathFollower intersects dest then  --if wrong dest. is reached, return tractor back and play instructions again
  92. --      puppetSprite ppathFollower,false
  93.       return(getOne(pdestinationList,dest)&".wrong")
  94.     end if
  95.   end repeat
  96.   return("")
  97. end
  98.  
  99.  
  100. on boundryIntoCoordinate me, boundry,spr
  101.   set coord=0
  102.   do "set coord=the "&boundry&" of sprite "&spr
  103.   return(coord)
  104. end
  105.  
  106.  
  107. on getDestinationName me
  108.   set dest=getAt(pdestinationList,pcurrentDestination)
  109.   return(getOne(pdestinationList,dest))
  110. end
  111.